﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SMS.Account
{
    public partial class Register : System.Web.UI.Page
    {
        SMS.Business.Users.UsersBusiness UB;
        SMS.Business.Roles.RolesBusiness RB;
        bool IsUpdate = false;


        protected void Page_Load(object sender, EventArgs e)
        {
            //   if (Session["CurrentUser"] == null)
            //     Response.Redirect("Login.aspx");
            UB = new Business.Users.UsersBusiness();
            RB = new Business.Roles.RolesBusiness();

            if (!IsPostBack)
            {
                drpRoles.DataSource = RB.Search(new DAL.Providers.RoleSearchCriteria());
                drpRoles.DataTextField = "Name";
                drpRoles.DataValueField = "ID";
                drpRoles.DataBind();
                if (Request.QueryString["UserID"] != null)
                {
                    var UserFound = UB.GetByID(int.Parse(Helper.QueryEncoder.Decrypt(Request.QueryString["UserID"].ToString())));
                    txtAdress.Text = UserFound.Address;
                    txtEmail.Text = UserFound.Email;
                    txtMobile.Text = UserFound.Mobile.ToString();
                    txtName.Text = UserFound.Name;
                    txtPass.Text = UserFound.Password;
                    txtPhone.Text = UserFound.Phone;
                    txtUserName.Text = UserFound.UserName;
                    drpRoles.Items.FindByValue(UserFound.RoleID.ToString()).Selected = true;
                    IsUpdate = true;
                    btnSave.Text = "تحديث";
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!UB.CheckUserNameAvailabilty(txtUserName.Text))
            {
                lblREsult.Text = "الرجاء تغير اسم الدخول لانه موجود";
            }
            else
            {
                if (!IsUpdate)
                {
                    UB.Insert(new DAL.User()
                    {
                        UserName = txtUserName.Text,
                        Address = txtAdress.Text,
                        Phone = txtPhone.Text,
                        Password = txtPass.Text,
                        Name = txtName.Text,
                        Mobile = txtMobile.Text,
                        RoleID = int.Parse(drpRoles.SelectedValue),
                        Email = txtEmail.Text

                    });
                    lblREsult.Text = "تم ادخل العميل بنجاح";

                }
                else
                {
                    UB.Update(new DAL.User()
                    {
                        UserName = txtUserName.Text,
                        Address = txtAdress.Text,
                        Phone = txtPhone.Text,
                        Password = txtPass.Text,
                        Name = txtName.Text,
                        Mobile = txtMobile.Text,
                        RoleID = int.Parse(drpRoles.SelectedValue),
                        Email = txtEmail.Text,
                        ID = int.Parse(Request.QueryString["UserID"])
                    });
                    lblREsult.Text = "تم تحديث بيانات الأعمل بنجاح";


                }
            }
        }

    }
}
